package com.sdy.auth.biz.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sdy.mvc.service.impl.BaseServiceImpl;
import com.sdy.auth.biz.mapper.SysUserMapper;
import com.sdy.auth.biz.model.SysUser;
import com.sdy.auth.biz.model.SysUserDept;
import com.sdy.auth.biz.service.SysUserDeptService;
import com.sdy.auth.biz.service.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户基础信息表 服务实现类
 * </p>
 *
 * @author zzq
 * @since 2019-06-14
 */
@Slf4j
@Service
public class SysUserServiceImpl extends BaseServiceImpl<SysUser> implements SysUserService {
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private SysUserDeptService sysUserDeptService;

    @Override
    public void saveUser(SysUser sysUser) {
        save(sysUser);
        sysUserDeptService.save(new SysUserDept().setUserId(sysUser.getId()).setAdmin(2).setCreateTime(new Date()).setDeptId(sysUser.getDeptId()));
    }

    @Override
    public void updateWithNullTime(SysUser user) {
        updateById(user);
        if (user.getExpireTime() == null) {
            sysUserMapper.updateExpireTimeNull(user.getId());
        }
        if (user.getDeptId() != null) {
            // 用户部门
            List<SysUserDept> rootUserDeptList = sysUserDeptService.listByExample(new SysUserDept().setUserId(user.getId()).setAdmin(2));
            List<Integer> toAddDeptIdList = new ArrayList<>();
            if (rootUserDeptList.stream().noneMatch(item -> item.getDeptId().equals(user.getDeptId()))) {
                toAddDeptIdList.add(user.getDeptId());
            }
            List<Integer> toDelUserDeptIdList = rootUserDeptList.stream()
                    .filter(item -> !item.getDeptId().equals(user.getDeptId()))
                    .map(SysUserDept::getId).collect(Collectors.toList());
            if (!toAddDeptIdList.isEmpty()) {
                toAddDeptIdList.forEach(item -> {
                    sysUserDeptService.save(new SysUserDept().setUserId(user.getId()).setAdmin(2).setCreateTime(new Date()).setDeptId(user.getDeptId()));
                });
            }
            if (!toDelUserDeptIdList.isEmpty()) {
                sysUserDeptService.removeByIds(toDelUserDeptIdList);
            }
        }
    }
    
    @Override
    public IPage<SysUser> pageUserWithRole(IPage<SysUser> page, Map<String, Object> query, Integer roleId, Boolean contain) {
        query.put("startRecord", (page.getCurrent() - 1) * page.getSize());
        query.put("endRecord", page.getSize());
        query.put("roleId", roleId);

        if (contain) {
            Integer count = sysUserMapper.pageUserWithRoleCount(query);
            if (count > 0) {
                List<SysUser> list = sysUserMapper.pageUserWithRole(query);
                return new Page<SysUser>(page.getCurrent(), page.getSize(), count).setRecords(list);
            } else {
                return new Page<SysUser>(page.getCurrent(), page.getSize(), count).setRecords(new ArrayList<>());
            }
        } else {
            Integer count = sysUserMapper.pageUserWithoutRoleCount(query);
            if (count > 0) {
                List<SysUser> list = sysUserMapper.pageUserWithoutRole(query);
                return new Page<SysUser>(page.getCurrent(), page.getSize(), count).setRecords(list);
            } else {
                return new Page<SysUser>(page.getCurrent(), page.getSize(), count).setRecords(new ArrayList<>());
            }
        }
    }

    @Override
    public void modifyPassword(Integer userId, String password) {
        SysUser vo = new SysUser();
        vo.setId(userId);
        vo.setPassword(password);
        sysUserMapper.updateById(vo);
    }
}
